const Redis=require('ioredis');
const redis=new Redis({db:5});
const config=require('../config/config');

module.exports={
    addCode:({client_id,code})=>{//流程A申请的授权码
        return redis.setex(join('code',client_id,code),config.auth.code,new Date().toLocaleString());
    },
    findCode:({client_id,code})=>{
        return redis.get(join('code',client_id,code));
    },
    delCode:({client_id,code})=>{
        return redis.del(join('code',client_id,code));
    },
    addToken:({client_id,token})=>{//流程D申请的令牌
        return redis.setex(join('token',client_id,token),config.auth.token,new Date().toLocaleString());
    },
    findToken:({client_id,token})=>{
        return redis.get(join('token',client_id,token));
    }
};

function join(...ns) {
    ns.unshift(config.name);
    return ns.join(':');
}